<?php

namespace app\models\product;

use app\models\constant\Status;
use Yii;

/**
 * This is the model class for table "product_sku_name".
 *
 * @property int $id
 * @property int $product_id 商品
 * @property string $name 名称
 * @property int $sort 排序
 * @property int $status 1开 0关 -99删除
 * @property string $other 其它属性,以备后用,如icon
 * @property string $create_time
 * @property string $update_time
 */
class ProductSkuName extends \yii\db\ActiveRecord
{
    use Status;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'product_sku_name';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'sort', 'status'], 'integer'],
            [['other'], 'safe'],
            [['create_time', 'update_time'], 'safe'],
            [['name', 'other'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'product_id' => 'Product ID',
            'name' => 'Name',
            'sort' => 'Sort',
            'status' => 'Status',
            'other' => 'Other',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * Save timestamp
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    /**
     * with value
     * @return \yii\db\ActiveQuery
     */
    public function getValues()
    {
        return $this->hasMany(ProductSkuValue::class, ['product_sku_name_id' => 'id'])
            ->where(['status' => ProductSkuValue::$statusMapping['OK']]);
    }
}
